package com.yyk.springgateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author ljp
 * @CLASSNAME GenericAccessResolver
 * @date 2021/2/2 16:30
 * @description： 按照访问地址进行限流(也可以安装其他条件进行限流)，具体可以看exchange.getRequest()的方法和属性
 */

public class GenericAccessResolver implements KeyResolver {
    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        return Mono.just(exchange.getRequest().getPath().value());
        // return Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
        // return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
    }

    @Bean
    public GenericAccessResolver hostAddrKeyResolver() {
        return new GenericAccessResolver();
    }

}
